home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 815 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. From: clamage@Eng.Sun.COM (Steve Clamage)
  2. Message-ID: <4is1a5$cag@engnews1.Eng.Sun.COM>
  3. X-Original-Date: 21 Mar 1996 16:49:09 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 22 Mar 96 02:05:40 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Referencing pointers after delete
  9. Organization: Sun Microsystems Inc.
  10. References: <4is05t$ceo@engnews1.Eng.Sun.COM>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMVILBeEDnX0m9pzZAQGdQgF/UkoOA7l3pdx2FjsEaJMGsFTsfC+xf3AF
  14.     5V5ubutqSCC7Zu1X7EQ/EINCnFlWQe0R
  15.     =BArS
  16.  
  17. In article ceo@engnews1.Eng.Sun.COM, "joe (j.) halpin" <jhalpin@bnr.ca>
  18. writes:
  19. >
  20. >In 3.7.3.2.4 the January working paper says:
  21. >
  22. >4 A deallocation function can free the storage referenced by the pointer
  23. >  given  as  its  argument and renders the pointer invalid.  The storage
  24. >  can be made available for further allocation.  An invalid pointer con-
  25. >  tains an unusable value:  it cannot even be used in an expression.
  26. >
  27. >This sounds as though, in the following:
  28. >
  29. >char *pc = new char[128];
  30. >delete pc;
  31. >pc = 0;
  32. >
  33. >it makes the final assignment (an expression) invalid.
  34.  
  35. No. It is not the pointer object that becomes unusable, but the value
  36. of the pointer. You can always store a valid value (like a null pointer)
  37. into the object.
  38.  
  39. The reason for the odd-looking condition (which follows the C
  40. standard) is to allow architectures which validate pointers in
  41. hardware to be standard-conforming. Example:
  42.  
  43. Suppose a "tagged" architecture marks each pointer as to whether it
  44. contains a valid value.
  45.     char *p; // p initially tagged as invalid value
  46.     p = new char[100]; // p tagged as valid
  47.     delete [] p; // p tagged as invalid
  48.     p = 0; // p tagged as valid null pointer
  49. The hardware might trap any use of an invalid pointer value, even just
  50. reading the value without dereferencing it.
  51.  
  52. You can't do anything useful in portable code with uninitialized pointers,
  53. or pointers which point to objects which are no longer available. The
  54. standard allows the implementation a great deal of leeway in trying to
  55. help by identifying such invalid uses.
  56.  
  57. ---
  58. Steve Clamage, stephen.clamage@eng.sun.com
  59. ---
  60. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  61. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  62. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  63. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  64. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  65.